import 'package:aichat_flutter_wallet/module/module_browser/viewmodel/we3_method.dart';
import 'package:aichat_flutter_wallet/module_common/utils/colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';

class DappNodeWidget extends StatefulWidget {
  Function? onChange;
  NodeInfo node;
  bool isSelect;
  int index;
  DappNodeWidget(
      {super.key,
      this.onChange,
      required this.node,
      this.isSelect = false,
      this.index = 0});

  @override
  State<DappNodeWidget> createState() => _DappNodeWidgetState();
}

class _DappNodeWidgetState extends State<DappNodeWidget> {
  Map<String, dynamic> blockMap = {};
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    doAsyncInit();
  }

  doAsyncInit() async {
    var blockMap1 = await Web3MethodCall.getBlockHeight(widget.node.url);
    setState(() {
      blockMap = blockMap1;
    });
  }

  Widget rateWidget() {
    return Row(
      children: [
        Text(
          '${blockMap['rate']}ms',
          style: TextStyle(color: blockMap['color'], fontSize: 10.sp),
        ),
        SizedBox(
          width: 10.w,
        ),
        Stack(
          children: [
            Transform.translate(
              offset: Offset(-2.25.w, 1.75.w),
              child: Container(
                height: 1.w,
                width: 9.w,
                color: blockMap['color'],
              ),
            ),
            Positioned(
              child: Container(
                height: 4.5.w,
                width: 4.5.w,
                decoration: BoxDecoration(
                    color: blockMap['color'],
                    borderRadius: BorderRadius.circular(5.w)),
              ),
            ),
          ],
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        widget.onChange!(widget.index, blockMap);
      },
      child: Container(
        width: double.infinity,
        margin: EdgeInsets.only(bottom: 10.w),
        padding: EdgeInsets.symmetric(horizontal: 15.w),
        decoration: BoxDecoration(
            color: widget.isSelect
                ? Color.fromRGBO(134, 103, 254, 0.05)
                : AppColors.c_F6F6F6,
            borderRadius: BorderRadius.all(Radius.circular(5.w)),
            border: widget.isSelect
                ? Border.all(width: 1.w, color: AppColors.c_8667FE)
                : null),
        height: 60.w,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    children: [
                      Text(
                        widget.node.name ?? 'Official',
                        style: TextStyle(
                            fontSize: 14.sp,
                            color: AppColors.c_333333,
                            fontWeight: FontWeight.w500),
                      ),
                      SizedBox(
                        width: 10,
                      ),
                      if (blockMap.isNotEmpty) rateWidget()
                    ],
                  ),
                  SizedBox(
                    height: 3.w,
                  ),
                  Container(
                    child: Text(
                      widget.node.url,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: 10.sp,
                        color: AppColors.c_868391,
                        fontWeight: FontWeight.w400,
                      ),
                      maxLines: 1,
                    ),
                  )
                ],
              ),
            ),
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Text(
                  blockMap.isNotEmpty ? blockMap['height'] : '--',
                  style: TextStyle(
                      fontSize: 12.sp,
                      color: AppColors.c_333333,
                      fontWeight: FontWeight.w400),
                ),
                SizedBox(
                  height: 7.w,
                ),
                Text(
                  'Block height',
                  style: TextStyle(
                      fontSize: 10.sp,
                      fontWeight: FontWeight.w400,
                      color: AppColors.c_868391),
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}
